package com.vframework.config.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.vframework.config.annotation.admin.AdminRestController;
import com.vframework.config.annotation.api.ApiRestController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.stream.Collectors;
import java.util.stream.Stream;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile({"dev", "debug"})
public class SwaggerConfig {

    @Bean
    public Docket adminFrameworkApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("后台管理")
                .apiInfo(apiInfo("后台管理", "1.0"))
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(AdminRestController.class))
//                .paths(PathSelectors.regex("/admin.*"))
                .build().protocols(Stream.of("http").collect(Collectors.toSet()));
    }

    @Bean
    public Docket deliveryApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("前端API")
                .apiInfo(apiInfo("前端API", "1.0"))
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.rrz.hibate.api.delivery"))
                .apis(RequestHandlerSelectors.withClassAnnotation(ApiRestController.class))
//                .paths(PathSelectors.regex("/api.*"))
                .build().protocols(Stream.of("http").collect(Collectors.toSet()));
    }

    /**
     * 访问地址：http://ip:port/swagger-ui.html
     *
     * @return
     */
    private ApiInfo apiInfo(String title, String version) {
        return new ApiInfoBuilder()
                .title(title)
                .version(version)
                .build();
    }
}
